package com.fpt.app.demo.view;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.fpt.app.demo.R;
import com.fpt.app.demo.task.AudioTask;
public class WarriorEngine extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
    private MyView stv;
    private Button btnStart;
    private Button btnStop;
    private AudioTask mAudioTask;
    private Timer mTimer;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        stv=(MyView)findViewById(R.id.myView);
        stv.requestFocus();
        btnStart=(Button)findViewById(R.id.start);
        btnStop=(Button)findViewById(R.id.stop);
        btnStart.setOnClickListener(this);
        btnStop.setOnClickListener(this);
        mAudioTask=new AudioTask();
//        stv = new GameEngineView(this);
//        setContentView(stv);
//        stv.requestFocus();
    }
	public void onClick(View v) {
		// TODO Auto-generated method stub
		String tag=(String) v.getTag();
		if(tag.equalsIgnoreCase("btnStart")){
			Log.e("Linhnv","on btn Start 1");
			mAudioTask.startRecording();
			mTimer =new Timer();
			mTimer.scheduleAtFixedRate(new TimerTask() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					Log.e("Linhnv","timer timer");
					if(mAudioTask.getmMaxAmp()==32767L ){
						Log.e("Linhnv","mAudioTask.getmMaxAmp()");
//						mTimer.cancel();
//						mAudioTask.stopRecording();
					}
					mAudioTask.resetRecording();
					mAudioTask.startRecording();
				}
			} , 0, 2000);
			
			
		}if(tag.equalsIgnoreCase("btnStop")){
			Log.e("Linhnv","on btn Stop");			
			mTimer.cancel();
			mAudioTask.stopRecording();
		}
	}
	
	
	
}